<template>
  <div>
    <!-- 调拨入库-->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjTransferStock"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(2)" :disabled="isShow">批量入库</multiuse-button>
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="iswarehouse" :title="'选择仓库'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <warehouseModel @close="warehousecloseHandler" :list="selectList" />
    </BaseDialog>
    <BaseDialog :visible.sync="isEmit" :title="'编辑'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <emitModel :rowData="formData" @close="emitcloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="issearch" :title="'选择经销商'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="ownercloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, getStorageList, exportStorage } from '@/api/dealerSale/TransferManager/TransferStock/index.js';
import { dictionary } from '@/utils/dictMixin';
import warehouseModel from './warehouseModel';
import ownerSeacher from '../ownerSeacher';
import { exportExcelFile } from '@/utils';
import emitModel from './emitModel';
import Cookie from 'js-cookie';
export default {
  name: 'WarehouseMaintenance',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        applyStatus: 60121003,
        toDealer: Cookie.get('ownerCode')
      },
      fetchApi: getList,
      formData: {},
      iswarehouse: false,
      isEmit: false,
      issearch: false,
      fromDealer: '',
      selectList: [],
      isShow: true
    };
  },
  mixins: [dictionary],
  components: {
    warehouseModel,
    emitModel,
    ownerSeacher
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '调拨单号',
          fieldName: 'applyNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '调出方',
          fieldName: 'fromDealerName',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请输入'
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'RANGE_DATE',
          label: '调出申请时间',
          fieldName: 'startApplyDate|endApplyDate',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'RANGE_DATE',
          label: '审核通过时间',
          fieldName: 'startCheckDate|endCheckDate',
          placeholder: '请选择',
          initialValue: []
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'applyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6012'),
          editItems: this.createDictList('6012')
        },
        {
          title: '调拨单号',
          dataIndex: 'applyNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调出方',
          dataIndex: 'fromDealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调入方',
          dataIndex: 'toDealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '调出方申请时间',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '审批通过日期',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '调拨说明',
          dataIndex: 'applyRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    seacherFun() {
      this.issearch = true;
    },
    selectChangeHandle(row) {
      this.selectList = row;
      if (row.length > 0) {
        let is = false;
        row.map(x => {
          if (x.applyStatus != 60121003) {
            is = true;
          }
        });
        this.isShow = !is ? false : true;
      } else {
        this.isShow = true;
      }
    },
    // async getStorageList(){
    //   let res=await getStorageList()
    //   console.log(res,'res')
    //   this.topFilterList.map(x=>{
    //     if(x.fieldName==''){
    //       x.itemList=res.data.map(x=>({text:'',value:''}))
    //     }
    //   })
    // },
    addHandle(type) {
      this.iswarehouse = true;
    },
    changeHandle(params) {
      this.fetchParams.fromDealer = this.fromDealer;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data, type) {
      this.formData = data;
      this.isEmit = true;
    },
    emitcloseHandler(val) {
      this.isEmit = false;
      if (val) {
        this.fetchParams.fromDealer = this.fromDealer;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    warehousecloseHandler() {
      this.iswarehouse = false;
      this.fetchParams.fromDealer = this.fromDealer;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    ownercloseHandler(val) {
      if (val) {
        console.log(val);
        this.topFilterList.map(x => {
          if (x.fieldName == 'fromDealerName') {
            x.initialValue = val.dealerName;
            this.fromDealer = val.dealerCode;
          }
        });
      }
      this.issearch = false;
    },
    resetChange() {
      this.fromDealer = '';
      this.fetchParams.fromDealer = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
